package com.study.d012Components;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;

import com.study.utils.MySystemTray;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/** 系统托盘
 * 参考：https://blog.csdn.net/why_su/article/details/90703123
*@description
*@author xtayhame
*date 2020年3月19日
*version
*since v1.0
*/
public class SystemTrayMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		Button exitTary=new Button("退出到系统托盘");
		anchorPane.getChildren().add(exitTary);
		AnchorPane.setTopAnchor(exitTary, 50.0);
		AnchorPane.setLeftAnchor(exitTary, 50.0);

		/*//SystemTray.isSupported()
		//SystemTray  tray = SystemTray.getSystemTray();
		//Image image = Toolkit.getDefaultToolkit().getImage("src/icon/01.jpg");

		PopupMenu menu = new PopupMenu();
		MenuItem item1 = new MenuItem("退出");
		MenuItem item2 = new MenuItem("打开");
		menu.add(item1);
		menu.add(item2);

		TrayIcon trayIcon = new TrayIcon(image, "系统", menu);
		trayIcon.setImageAutoSize(true);
*/
		stage.setScene(new Scene(anchorPane, 400, 300));
		stage.show();

		exitTary.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				/*
				//设置不直接退出
				Platform.setImplicitExit(true);
				try {
					//主面板隐藏
					stage.hide();
					//添加图标
					tray.add(trayIcon);
				} catch (AWTException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/

				MySystemTray mySystemTray = MySystemTray.getInstance();
				mySystemTray.listen(stage);
				mySystemTray.hide(stage);
			}

		});

		//退出
		/*item1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent e) {
				//
				Platform.setImplicitExit(false);

				// Not on FX application thread; currentThread = AWT-EventQueue-0
				Platform.runLater(new Runnable() {
					@Override
					public void run() {
						System.out.println("exit");
						stage.close();
					}
				});
				System.out.println("are you want to exit.");

				System.out.println("clikc exit start.");
				Platform.runLater(new Runnable() {

					@Override
					public void run() {
						System.out.println("exit 。。。。。。。");
					}
				});
				System.out.println("clikc exit end.");

			}
		});

		//显示
		item2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent e) {
				//
				// Not on FX application thread; currentThread = AWT-EventQueue-0
				Platform.runLater(new Runnable() {
					@Override
					public void run() {
						System.out.println("show");
						stage.show();
						stage.toFront();
					}
				});
				tray.remove(trayIcon);
				System.out.println("show aciton.");
			}
		});
*/

	}
}
